package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.permission;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.AccountRoleMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmAccountRoleService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxueqian on 2017/1/6/006.
 * 角色人员绑定服务
 */

public class JsmAccountRoleServiceImpl implements JsmAccountRoleService {
    @Autowired
    private JsmRolesService rolesService;
    @Autowired
    private AccountRoleMapper accountRoleMapper;

    /**
     * @param employeeVo 登录人员信息
     * @param systemCode 系统编码
     * @return
     * @throws Exception
     */
    @Override
    public List<AccountRole> getRolesByAccount(String systemCode, EmployeeVo employeeVo) throws Exception {
        Map selectMap = new HashMap<>();
        //selectMap.put("systemCode", systemCode); 由于角色无法与子系统绑定， 取消原有的查询方式；
        selectMap.put("employeeId", employeeVo.getEmployeeId());
        return accountRoleMapper.selectAccountRole(selectMap);
    }

    /**
     * 根据当前用户信息 查询角色信息 ；
     *
     * @param employee 当前用户信息 ；
     */
    @Override
    public List<AccountRole> getRolesByAccount(Employee employee) throws Exception {
        Map selectMap = new HashMap<>();
        selectMap.put("employeeId", employee.getEmployeeId());
        return accountRoleMapper.selectAccountRole(selectMap);
    }

    /**
     * 角色人员绑定
     *
     * @param roleId      角色ID
     * @param accountList
     * @return
     * @throws Exception
     */
    @Override
    public Integer accountRoleAdd(String roleId, List<String> accountList) throws Exception {
        Roles roles = rolesService.rolesSelect(roleId);
        Integer resInt = 0;
        if (accountList != null && roleId != null) {
            for (String accountId : accountList) {
                AccountRole accountRoleOld = new AccountRole();
                accountRoleOld.setRoleId(roleId);
                accountRoleOld.setOperatorId(accountId);
                accountRoleOld.setStatus(new BigDecimal(0));
                accountRoleOld = accountRoleMapper.selectByAccountRole(accountRoleOld);
                if (accountRoleOld == null) {
                    AccountRole accountRole = new AccountRole();
                    accountRole.setAccountRoleId(UniqueUtil.uuid());
                    accountRole.setRoleId(roleId);
                    accountRole.setOperatorId(accountId);
                    accountRole.setStatus(BigDecimal.valueOf(0));
                    accountRole.setSubsystem(roles.getSystemCode());
                    resInt = resInt + accountRoleMapper.insertSelective(accountRole);
                }
            }
        }
        return resInt;
    }

    /**
     * 角色人员取消绑定
     *
     * @param roleId      角色ID
     * @param accountList 账号id集合
     * @return
     * @throws Exception
     */
    @Override
    public Integer accountRoleDel(String roleId, List<String> accountList) throws Exception {
        Integer resInt = 0;
        if (accountList != null && roleId != null) {
            for (String accountId : accountList) {
                Map delMap = new HashMap<>();
                delMap.put("operatorId", accountId);
                delMap.put("roleId", roleId);
                resInt = resInt + accountRoleMapper.deleteByRAndAccountId(delMap);
            }
        }
        return resInt;
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<AccountRole> list) {
        return accountRoleMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<AccountRole> list) {
        return accountRoleMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:17
     */
    @Override
    public int deleteList(List<AccountRole> list) {
        return accountRoleMapper.deleteList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 根据条件查询ID集合
     * @Date 2017/2/8 17:17
     */
    @Override
    public List<String> selectByListId(List<AccountRole> list) {
        return accountRoleMapper.selectByListId(list);
    }

    @Override
    public List<AccountRole> getRolesByAccount(EmployeeVo employeeVo) throws Exception {
        Map selectMap = new HashMap<>();
        selectMap.put("employeeId", employeeVo.getEmployeeId());
        return accountRoleMapper.selectAccountRole(selectMap);
    }
}
